﻿using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using EasyXaf.Sample.Module.BusinessObjects;

namespace EasyXaf.Sample.Module.Controllers;

public class UserViewController : ObjectViewController<DetailView, ApplicationUser>
{
    public SimpleAction SetImageAction { get; }

    public SimpleAction SetImage2Action { get; }

    public UserViewController()
    {
        SetImageAction = new SimpleAction(this, "SetImageAction", PredefinedCategory.Edit)
        {
            Caption = "设置图像01"
        };

        SetImage2Action = new SimpleAction(this, "SetImage2Action", PredefinedCategory.Edit)
        {
            Caption = "设置图像02"
        };
    }

    protected override void OnActivated()
    {
        base.OnActivated();

        SetImageAction.Execute += SetImageAction_Execute;
        SetImage2Action.Execute += SetImageAction_Execute;
    }

    protected override void OnDeactivated()
    {
        SetImageAction.Execute -= SetImageAction_Execute;
        SetImage2Action.Execute -= SetImageAction_Execute;

        base.OnDeactivated();
    }

    private void SetImageAction_Execute(object sender, SimpleActionExecuteEventArgs e)
    {
        if (sender == SetImageAction)
        {
            ViewCurrentObject.ImageUrl = "/images/image01.jpg";
        }
        else if (sender == SetImage2Action)
        {
            ViewCurrentObject.ImageUrl = "/images/image02.jpg";
        }
    }
}
